library(tidyr)
library(tidyverse)
library(lubridate)
library(dplyr)
library(ggplot2)
library(gganimate)
library(gifski) # this helps gganimate to turn png into videos/gifs
library(png) # see above note
library(readr)
library(scales) # package for scale formatting
## # A tibble: 6 x 11
## country_region_… country_region sub_region_1 sub_region_2 date
## <fct> <fct> <fct> <fct> <fct>
## 1 AE United Arab E… "" "" 2020…
## 2 AE United Arab E… "" "" 2020…
## 3 AE United Arab E… "" "" 2020…
## 4 AE United Arab E… "" "" 2020…
## 5 AE United Arab E… "" "" 2020…
## 6 AE United Arab E… "" "" 2020…
## # … with 6 more variables:
## # retail_and_recreation_percent_change_from_baseline <int>,
## # grocery_and_pharmacy_percent_change_from_baseline <int>,
## # parks_percent_change_from_baseline <int>,
## # transit_stations_percent_change_from_baseline <int>,
## # workplaces_percent_change_from_baseline <int>,
## # residential_percent_change_from_baseline <int>
# Create a new data frame with only Pennsylvania mobility data
PA_community_mobility <- subset(Global_Mobility_Report,
sub_region_1 == 'Pennsylvania',
select = c("sub_region_1",
"sub_region_2",
"date",
"retail_and_recreation_percent_change_from_baseline",
"grocery_and_pharmacy_percent_change_from_baseline",
"parks_percent_change_from_baseline",
"transit_stations_percent_change_from_baseline",
"workplaces_percent_change_from_baseline",
"residential_percent_change_from_baseline"))
# Change date column to as.date so R can read it as a date
PA_community_mobility$date <- as.Date(PA_community_mobility$date)
STATIC PLOT
# Static retail and recreation mobility plot:
staticPlot_RetailRecreation_mobilitychange <-
ggplot(data=PA_community_mobility)+
geom_point(mapping=aes(x=date,
y=retail_and_recreation_percent_change_from_baseline,
color=retail_and_recreation_percent_change_from_baseline))+
theme_minimal() +
scale_color_gradient(low = "#0091ff",
high = "#f0650e") +
theme_linedraw()
# Display the plot:
print(staticPlot_RetailRecreation_mobilitychange) + labs(x="Date", y="Retail and Recreation Mobility Change in PA")
ANIMATED PLOT
# Animated retail and recreation mobility plot:
animPlot_RetailRecreation_mobilitychange <-
ggplot(PA_community_mobility,
aes(x=date,
y=retail_and_recreation_percent_change_from_baseline)) +
geom_point(aes(color=retail_and_recreation_percent_change_from_baseline))
animPlot_RetailRecreation_mobilitychange <-
animPlot_RetailRecreation_mobilitychange+
transition_time(PA_community_mobility$date) +
shadow_mark() +
scale_x_date(labels=date_format("%m/%d")) +
xlab("Date") +
ylab("Retail and Recreation Mobility Change in PA")+
scale_color_gradient(low = "#0091ff",
high = "#f0650e")+
theme(legend.position="none") +
theme_linedraw() +
theme(legend.position="none")
# Display the plot:
animated_RetailRecreation_plot <- animate(animPlot_RetailRecreation_mobilitychange, width=700, height=500)
animated_RetailRecreation_plot
Static plot
# Static grocery and pharmacy mobility plot:
staticPlot_GroceryPharmacy_mobilitychange <-
ggplot(data=PA_community_mobility)+
geom_point(mapping=aes(x=date,
y=grocery_and_pharmacy_percent_change_from_baseline,
color=grocery_and_pharmacy_percent_change_from_baseline))+
theme_minimal() +
scale_color_gradient(low = "#0091ff",
high = "#f0650e") +
theme_linedraw()
# Display the plot:
print(staticPlot_GroceryPharmacy_mobilitychange) + labs(x="Date", y="Grocery and Pharmacy Mobility Change in PA")
ANIMATED PLOT
# Animated grocery and pharmacy mobility plot:
animPlot_GroceryPharmacy_mobilitychange <-
ggplot(PA_community_mobility,
aes(x=date,
y=grocery_and_pharmacy_percent_change_from_baseline)) +
geom_point(aes(color=grocery_and_pharmacy_percent_change_from_baseline))
animPlot_GroceryPharmacy_mobilitychange <-
animPlot_GroceryPharmacy_mobilitychange+
transition_time(PA_community_mobility$date) +
shadow_mark() +
scale_x_date(labels=date_format("%m/%d")) +
xlab("Date") +
ylab("Grocery and Pharmacy Mobility Change in PA")+
scale_color_gradient(low = "#0091ff",
high = "#f0650e")+
theme(legend.position="none") +
theme_linedraw() +
theme(legend.position="none")
# Display the plot:
animated_GroceryPharmacy_plot <- animate(animPlot_GroceryPharmacy_mobilitychange, width=700, height=500)
animated_GroceryPharmacy_plot
Static plot
# Static parks mobility plot:
staticPlot_Parks_mobilitychange <-
ggplot(data=PA_community_mobility)+
geom_point(mapping=aes(x=date,
y=parks_percent_change_from_baseline,
color=parks_percent_change_from_baseline))+
theme_minimal() +
scale_color_gradient(low = "#0091ff",
high = "#f0650e") +
theme_linedraw()
# Display the plot:
print(staticPlot_Parks_mobilitychange) + labs(x="Date", y="Parks Mobility Change in PA")
Animated plot
# Animated parks mobility plot:
animPlot_Parks_mobilitychange <-
ggplot(PA_community_mobility,
aes(x=date,
y=parks_percent_change_from_baseline)) +
geom_point(aes(color=parks_percent_change_from_baseline))
animPlot_Parks_mobilitychange <-
animPlot_Parks_mobilitychange+
transition_time(PA_community_mobility$date) +
shadow_mark() +
scale_x_date(labels=date_format("%m/%d")) +
xlab("Date") +
ylab("Parks Mobility Change in PA")+
scale_color_gradient(low = "#0091ff",
high = "#f0650e")+
theme(legend.position="none")
# Display the plot:
animated_Parks_plot <- animate(animPlot_Parks_mobilitychange, width=700, height=500)
animated_Parks_plot
Static plot
# Static transit stations mobility plot:
staticPlot_TransitStations_mobilitychange <-
ggplot(data=PA_community_mobility)+
geom_point(mapping=aes(x=date,
y=transit_stations_percent_change_from_baseline,
color=transit_stations_percent_change_from_baseline))+
theme_minimal() +
scale_color_gradient(low = "#0091ff",
high = "#f0650e") +
theme_linedraw()
# Display the plot:
print(staticPlot_TransitStations_mobilitychange) + labs(x="Date", y="Transit Stations Mobility Change in PA")
Animated plot
# Animated transit stations mobility plot:
animPlot_TransitStations_mobilitychange <-
ggplot(PA_community_mobility,
aes(x=date,
y=transit_stations_percent_change_from_baseline)) +
geom_point(aes(color=transit_stations_percent_change_from_baseline))
animPlot_TransitStations_mobilitychange <-
animPlot_TransitStations_mobilitychange+
transition_time(PA_community_mobility$date) +
shadow_mark() +
scale_x_date(labels=date_format("%m/%d")) +
xlab("Date") +
ylab("Transit Stations Mobility Change in PA")+
scale_color_gradient(low = "#0091ff",
high = "#f0650e")+
theme(legend.position="none") +
theme_linedraw()
# Display the plot:
animated_TransitStations_plot <- animate(animPlot_TransitStations_mobilitychange,
width=700,
height=500)
animated_TransitStations_plot
Static plot
# Static workplace mobility plot:
staticPlot_Workplaces_mobilitychange <-
ggplot(data=PA_community_mobility)+
geom_point(mapping=aes(x=date,
y=workplaces_percent_change_from_baseline,
group=workplaces_percent_change_from_baseline,
color=workplaces_percent_change_from_baseline))+
theme_minimal() +
scale_color_gradient(low = "#0091ff",
high = "#f0650e") +
theme_linedraw()
# Display the plot:
print(staticPlot_Workplaces_mobilitychange) + labs(x="Date", y="Workplaces Mobility Change in PA")
Animated plot
# Animated workplace mobility plot:
animPlot_Workplaces_mobilitychange <-
ggplot(PA_community_mobility,
aes(x=date,
y=workplaces_percent_change_from_baseline)) +
geom_point(aes(color=workplaces_percent_change_from_baseline))
animPlot_Workplaces_mobilitychange <-
animPlot_Workplaces_mobilitychange+
transition_time(PA_community_mobility$date) +
shadow_mark() +
scale_x_date(labels=date_format("%m/%d")) +
xlab("Date") +
ylab("Workplace Mobility Change in PA")+
scale_color_gradient(low = "#0091ff",
high = "#f0650e")+
theme(legend.position="none")+
theme_linedraw()
# Display the plot:
animated_Workplaces_plot <- animate(animPlot_Workplaces_mobilitychange, width=700, height=500)
animated_Workplaces_plot
Static plot
# Residential area mobility plot:
staticPlot_Residential_mobilitychange <-
ggplot(data=PA_community_mobility)+
geom_point(mapping=aes(x=date,
y=residential_percent_change_from_baseline,
color=residential_percent_change_from_baseline))+
theme_minimal() +
scale_color_gradient(low = "#0091ff",
high = "#f0650e") +
theme_linedraw()
# Display the plot:
print(staticPlot_Residential_mobilitychange) + labs(x="Date", y="Residential Area Mobility Change in PA")
Residential Area mobility change in Pennsylvania from 02/15/2020 to 04/10/2020 (Animated plot)
# Animated residential mobility plot:
animPlot_Residential_mobilitychange <-
ggplot(PA_community_mobility,
aes(x=date,
y=residential_percent_change_from_baseline)) +
geom_point(aes(color=residential_percent_change_from_baseline))
animPlot_Residential_mobilitychange <-
animPlot_Residential_mobilitychange+
transition_time(PA_community_mobility$date) +
shadow_mark() +
scale_x_date(labels=date_format("%m/%d")) +
xlab("Date") +
ylab("Residential Area Mobility Change in PA")+
scale_color_gradient(low = "#0091ff",
high = "#f0650e")+
theme(legend.position="none") +
theme_linedraw()
# Display the plot:
animated_Residential_plot <- animate(animPlot_Residential_mobilitychange,
width=700,
height=500)
animated_Residential_plot
Now I want to move these plots to a Shiny App